package cn.yunlang.zhangZhongFei.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import cn.yunlang.zhangZhongFei.entity.UserLike;
import cn.yunlang.zhangZhongFei.service.UserLikeService;
import cn.yunlang.zhangZhongFei.util.JwtUtil;
import cn.yunlang.zhangZhongFei.vo.request.TokenRequest;
import cn.yunlang.zhangZhongFei.vo.request.UserLikePageVo;
import cn.yunlang.zhangZhongFei.vo.request.UserLikeVo;
import cn.yunlang.zhangZhongFei.vo.response.R;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/UserLike")
@Api(tags = "用户关注")
public class UserLikeController {

  @Resource private UserLikeService userLikeService;

  @ApiOperation("分页查 我的关注")
  @GetMapping("/pageLike")
  public R<IPage<UserLikePageVo>> pageLike(
      @RequestParam Integer pageNo,
      @RequestParam Integer pageSize,
      @RequestBody TokenRequest request) {
    Claims claims = new JwtUtil().parseJWT(request.getToken());
    Long userId = Long.valueOf(claims.getSubject());
    return userLikeService.pageLike(pageNo, pageSize, userId);
  }

  @ApiOperation("分页查 谁关注我")
  @GetMapping("/likedPage")
  public R<IPage<UserLike>> likedPage(
      @RequestParam Integer pageNo,
      @RequestParam Integer pageSize,
      @RequestBody TokenRequest request) {
    Claims claims = new JwtUtil().parseJWT(request.getToken());
    Long userId = Long.valueOf(claims.getSubject());
    return userLikeService.likedPage(pageNo, pageSize, userId);
  }

  @GetMapping("/one")
  @ApiOperation("根据Id查一条")
  public R<UserLike> one(@RequestParam Long userLikeId) {
    return userLikeService.getOne(userLikeId);
  }

  @PostMapping("/doLike")
  @ApiOperation("关注or取消关注")
  public R<String> doLike(@RequestBody UserLikeVo userLikeVo) {
    userLikeVo.setUserId(Long.valueOf(new JwtUtil().getUserId()));
    return userLikeService.doLike(userLikeVo);
  }

  @PostMapping("/like")
  @ApiOperation("关注")
  public R<String> like(@RequestBody UserLikeVo userLikeVo) {
    userLikeVo.setUserId(Long.valueOf(new JwtUtil().getUserId()));
    return userLikeService.likeUser(userLikeVo);
  }

  @PostMapping("/removeLike")
  @ApiOperation("取消关注")
  public R<String> removeLike(
      @RequestBody UserLikeVo userLikeVo) {
    userLikeVo.setUserId(Long.valueOf(new JwtUtil().getUserId()));
    return userLikeService.removeLikeUser(userLikeVo);
  }
}
